package com.hellogwt.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;
import com.hellogwt.client.event.EditGreetingCancelledEvent;
import com.hellogwt.client.event.EditGreetingCancelledEventHandler;
import com.hellogwt.client.event.EditGreetingEvent;
import com.hellogwt.client.event.EditGreetingEventHandler;
import com.hellogwt.client.presenter.EditGreetingPresenter;
import com.hellogwt.client.presenter.GreetingsPresenter;
import com.hellogwt.client.presenter.Presenter;
import com.hellogwt.client.service.GreetingServiceAsync;
import com.hellogwt.client.view.EditGreetingView;
import com.hellogwt.client.view.GreetingsView;

public class GreetingsController implements Presenter, ValueChangeHandler<String> {

    private static final String LIST_HISTORY_ITEM = "list";
    private static final String EDIT_HISTORY_ITEM = "edit";

    private HandlerManager eventBus = null;
    private GreetingServiceAsync greetingService = null;
    private HasWidgets container = null;

    public GreetingsController(GreetingServiceAsync greetingService, HandlerManager eventBus) {
        this.eventBus = eventBus;
        this.greetingService = greetingService;

        bind();
    }

    @Override
    public void go(final HasWidgets container) {
        this.container = container;

        if ("".equals(History.getToken())) {
            History.newItem(LIST_HISTORY_ITEM);
        } else {
            History.fireCurrentHistoryState();
        }
    }

    @Override
    public void onValueChange(ValueChangeEvent<String> event) {
        String token = event.getValue();

        if (token != null) {
            Presenter presenter = null;

            if (LIST_HISTORY_ITEM.equals(token)) {
                presenter = new GreetingsPresenter(greetingService, eventBus, new GreetingsView());
            } else if (EDIT_HISTORY_ITEM.equals(token)) {
                presenter = new EditGreetingPresenter(greetingService, eventBus, new EditGreetingView());
            }

            if (presenter != null) {
                presenter.go(container);
            }
        }
    }

    private void bind() {
        History.addValueChangeHandler(this);

        eventBus.addHandler(EditGreetingEvent.TYPE, new EditGreetingEventHandler() {
            @Override
            public void onEditGreeting(EditGreetingEvent event) {
                doEditGreeting();
            }
        });

        eventBus.addHandler(EditGreetingCancelledEvent.TYPE, new EditGreetingCancelledEventHandler() {
            @Override
            public void onEditGreetingCancelled(EditGreetingCancelledEvent event) {
                doEditGreetingCancelled();
            }
        });
    }

    private void doEditGreeting() {
        History.newItem(EDIT_HISTORY_ITEM);
    }

    private void doEditGreetingCancelled() {
        History.newItem(LIST_HISTORY_ITEM);
    }
}